package com.kl.mq;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
@RocketMQMessageListener(topic = "phone-topic",consumerGroup = "messageGroup")
public class CodeConsumer implements RocketMQPushConsumerLifecycleListener, RocketMQListener<String[]> {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void onMessage(String[] strings) {

    }

    @Override
    public void prepareStart(DefaultMQPushConsumer defaultMQPushConsumer) {
        defaultMQPushConsumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                MessageExt messageExt = list.get(0);

                int reconsumeTimes = messageExt.getReconsumeTimes();

                boolean mqmessage = redisTemplate.hasKey("phone");
                try {
                    if (!mqmessage){
                        byte[] body = messageExt.getBody();
                        String phone = new String(body);

                        DefaultProfile profile = DefaultProfile.getProfile("cn-beijing", "LTAI4GCbdwnjCGwQj9Z3tr3N", "27ih4pc1W1tqey9X4mmCy1JUotAX7L");


                        IAcsClient client = new DefaultAcsClient(profile);

                        CommonRequest request = new CommonRequest();

                        //请求方式
                        request.setSysMethod(MethodType.POST);
                        //调佣阿里大于的域名
                        request.setSysDomain("dysmsapi.aliyuncs.com");
                        //版本号
                        request.setSysVersion("2017-05-25");
                        //发生短信功能
                        request.setSysAction("SendSms");

                        //接收短信方的手机号
                        request.putQueryParameter("PhoneNumbers",phone);
                        //签名
                        request.putQueryParameter("SignName","东科创想");
                        //电信模板
                        request.putQueryParameter("TemplateCode","SMS_162522027");

                        String s = RandomStringUtils.randomNumeric(6);
                        redisTemplate.opsForValue().set(phone,s);

                        redisTemplate.expire(phone,30, TimeUnit.MINUTES);
                        request.putQueryParameter("TemplateParam","{\"sms_code\":\""+s+"\"}");

                        try {
                            CommonResponse response = client.getCommonResponse(request);
                            System.out.println(response.getData());
                        } catch (ServerException e) {
                            e.printStackTrace();
                        } catch (ClientException e) {
                            e.printStackTrace();
                        }
                    }
                } catch (Exception e) {
                    if (reconsumeTimes>2){
                        System.out.println("重试三次，消费失败");
                        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                    }
                    return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
    }
}
